<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends MY_Controller {
     var $_templates=array();
	 var $lang='';
    function __construct() {
        parent::__construct();
        $this->session->set_userdata(array('Url'=>uri_string()));
        
        $this->_lang = $this->lang->lang();
        $this->load->library('securimage');
        $this->load->helper('phpmailer');
        $this->load->model('contact_model','contact');
        $this->load->model('email_model','email');
    }
	public function index()
	{
		
        $this->_templates['page'] = 'contact/index';
		// page title
		$data['title_page'] = "Thông tin liên hệ";
        $this->site_lib->load($this->_templates['page'],$data);
	}
    private function data_contact()
    {
        $arr_data = array();
        $arr_data['full_name']=$this->input->post('full_name');
        $arr_data['company']=$this->input->post('company');
        $arr_data['phone']=$this->input->post('phone');
        $arr_data['address']=$this->input->post('address');
        $arr_data['email']=$this->input->post('email');
        $arr_data['content']=$this->input->post('content');
        $arr_data['dt_create']=date("Y-m-d H:i:s");
        return $arr_data;
    }
    function send_contact()
    {
        $data['success']=true;
        $this->form_validation->set_rules('full_name',lang('contact.full_name'),'trim|required');
        $this->form_validation->set_rules('email',lang('contact.email'),'trim|required|valid_email');
        $this->form_validation->set_rules('content',lang('contact.content'),'trim|required');
            
        if($this->form_validation->run() == FALSE){
           $this->pre_message = validation_errors();
           $data['str_respon'] = $this->pre_message;
           $data['success']=false;
        }else{
            $code = $this->input->post('code');
            if(!$this->securimage->check($code)){
                $data['str_respon'] = lang('contact.captcha_incorrect');
                $data['success'] = false;
                
            }else{
                $data_contact=$this->data_contact();
                $id_contact = $this->contact->saveContact(0,$data_contact);
                if($id_contact){
                    /*Begin send email*/
                    $data_mail['user_name']=$this->input->post('lb_full_name');
                    $data_mail['contact_mail']= $this->config->item('contact_email');
                    $data_mail['admin_email'] = $this->config->item('admin_email');
                    $data_mail['site_name']   = $this->config->item('site_name');
                    $data_mail['site_url']    = $this->config->item('base_url');
                    
                    $rs_template = $this->email->getEmailTemplate('customer_contact');
                    if($rs_template){
                        $full_name = $data_contact['full_name'];
                        $company = $data_contact['company'];
                        $address = $data_contact['address'];
                        $phone = $data_contact['phone'];
                        $email = $data_contact['email'];
                        $content= $data_contact['content'];
                        
                        $subject = $rs_template->subject;
                        $template_text=addslashes($rs_template->content);
                        eval("\$message=\"$template_text\";");
                        
                        $rs_email= send_email($this->config->item('admin_email'),$this->config->item('contact_email'),$data_contact['email'], $subject, $message);
                        if($rs_email)
                        {
                            $this->contact->saveContact($id_contact,array('bl_send'=>1));
                        }
                    }
                   
                    //End send email
                        
                    $data['success']=true;
                    $data['str_respon']='';
                }else{
                    $data['success']=false;
                    $data['str_respon']='';
                }
           }
        }
        echo json_encode($data); 
    }
}

/* End of file contact.php */
/* Location: ./application/site/controllers/contact.php */